package cn.iocoder.yudao.module.train.dal.mysql.coursecommodity;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.train.controller.admin.staff.vo.StaffPageReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.WxUser.WxUserDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StaffUserDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.course.CourseDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.module.train.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.train.dal.dataobject.teacher.TeacherDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.train.controller.admin.coursecommodity.vo.*;

/**
 * 课程商品管理 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface CourseCommodityMapper extends BaseMapperX<CourseCommodityDO> {

//    default PageResult<CourseCommodityDO> selectPage(CourseCommodityPageReqVO reqVO) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<CourseCommodityDO>()
//                .eqIfPresent(CourseCommodityDO::getCourseId, reqVO.getCourseId())
//                .eqIfPresent(CourseCommodityDO::getTeacherId, reqVO.getTeacherId())
//                .eqIfPresent(CourseCommodityDO::getCourseCoverUrl, reqVO.getCourseCoverUrl())
//                .eqIfPresent(CourseCommodityDO::getCommodityPrice, reqVO.getCommodityPrice())
//                .betweenIfPresent(CourseCommodityDO::getTeachingAllTime, reqVO.getTeachingAllTime())
//                .betweenIfPresent(CourseCommodityDO::getTeachingTime, reqVO.getTeachingTime())
//                .betweenIfPresent(CourseCommodityDO::getBeginTime, reqVO.getBeginTime())
//                .betweenIfPresent(CourseCommodityDO::getEndTime, reqVO.getEndTime())
//                .eqIfPresent(CourseCommodityDO::getVipFlag, reqVO.getVipFlag())
//                .eqIfPresent(CourseCommodityDO::getApplicantNumber, reqVO.getApplicantNumber())
//                .eqIfPresent(CourseCommodityDO::getCourseFlag, reqVO.getCourseFlag())
//                .betweenIfPresent(CourseCommodityDO::getCreateTime, reqVO.getCreateTime())
//                .orderByDesc(CourseCommodityDO::getId));
//    }

    default PageResult<CourseCommodityDTO> selectPage(CourseCommodityPageReqVO reqVO) {
        // 创建MyBatis-Plus分页对象
        Page<CourseCommodityDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());

        // 创建连接查询条件
        MPJLambdaWrapper<CourseCommodityDO> wrapper = new MPJLambdaWrapper<CourseCommodityDO>()
                .selectAll(CourseCommodityDO.class)
                .select(CourseDO::getName)
                .leftJoin(CourseDO.class, on -> on
                        .eq(CourseDO::getId, CourseCommodityDO::getCourseId)
                )
                .select(TeacherDO::getTeacherName)
                .leftJoin(TeacherDO.class,on -> on
                        .eq(TeacherDO::getId,CourseCommodityDO::getTeacherId));  // 连接条件

        // 添加与原方法相同的过滤条件
        if (reqVO.getName() != null) {
            wrapper.like(CourseDO::getName, reqVO.getName());
        }
        if (reqVO.getTeacherName() != null) {
            wrapper.like(TeacherDO::getTeacherName, reqVO.getTeacherName());
        }
        if (reqVO.getCourseCoverUrl() != null) {
            wrapper.eq(CourseCommodityDO::getCourseCoverUrl, reqVO.getCourseCoverUrl());
        }
        if (reqVO.getCommodityPrice() != null) {
            wrapper.eq(CourseCommodityDO::getCommodityPrice, reqVO.getCommodityPrice());
        }
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(CourseCommodityDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        if (reqVO.getCommodityName() != null) {
            wrapper.eq(CourseCommodityDO::getCommodityName, reqVO.getCommodityName());
        }
        if (reqVO.getStatus() != null) {
            wrapper.eq(CourseCommodityDO::getStatus, reqVO.getStatus());
        }
        if (reqVO.getTeachingAllTime() != null) {
            wrapper.eq(CourseCommodityDO::getTeachingAllTime, reqVO.getTeachingAllTime());
        }
        if (reqVO.getTeachingTime() != null) {
            wrapper.eq(CourseCommodityDO::getTeachingTime, reqVO.getTeachingTime());
        }
        if (reqVO.getBeginTime() != null) {
            wrapper.eq(CourseCommodityDO::getBeginTime, reqVO.getBeginTime());
        }
        if (reqVO.getEndTime() != null) {
            wrapper.eq(CourseCommodityDO::getEndTime, reqVO.getEndTime());
        }
        if (reqVO.getVipFlag() != null) {
            wrapper.eq(CourseCommodityDO::getVipFlag, reqVO.getVipFlag());
        }
        if (reqVO.getApplicantNumber() != null) {
            wrapper.eq(CourseCommodityDO::getApplicantNumber, reqVO.getApplicantNumber());
        }
        if (reqVO.getCourseFlag() != null) {
            wrapper.eq(CourseCommodityDO::getCourseFlag, reqVO.getCourseFlag());
        }
        if (reqVO.getCreateTime() != null) {
            wrapper.eq(CourseCommodityDO::getCreateTime, reqVO.getCreateTime());
        }
        wrapper.orderByDesc(CourseCommodityDO::getId);

        // 执行连接查询并分页
        IPage<CourseCommodityDTO> resultPage = selectJoinPage(page, CourseCommodityDTO.class, wrapper);

        // 转换为框架的PageResult
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }

    default CourseCommodityDTO selectCourseCommodity(Long id) {
        return selectJoinOne(CourseCommodityDTO.class,
                new MPJLambdaWrapper<CourseCommodityDO>()
                        .selectAll(CourseCommodityDO.class)
                        .select(CourseDO::getName)
                        .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                        .select(TeacherDO::getTeacherName)  // 关键：别名必须是 uStatus（小写u）
                        .leftJoin(TeacherDO.class, TeacherDO::getId, CourseCommodityDO::getTeacherId)
                        .eq(CourseCommodityDO::getId, id)
        );
    }

    default List<CourseCommodityDTO> selectList(CourseCommodityPageReqVO reqVO) {
        // 创建连接查询条件
        MPJLambdaWrapper<CourseCommodityDO> wrapper = new MPJLambdaWrapper<CourseCommodityDO>()
                .selectAll(CourseCommodityDO.class)
                .select(CourseDO::getName)
                .leftJoin(CourseDO.class, on -> on
                        .eq(CourseDO::getId, CourseCommodityDO::getCourseId)
                )
                .select(TeacherDO::getTeacherName)
                .leftJoin(TeacherDO.class,on -> on
                        .eq(TeacherDO::getId,CourseCommodityDO::getTeacherId));  // 连接条件

        // 添加与原方法相同的过滤条件
        if (reqVO.getName() != null) {
            wrapper.like(CourseDO::getName, reqVO.getName());
        }
        if (reqVO.getTeacherName() != null) {
            wrapper.like(TeacherDO::getTeacherName, reqVO.getTeacherName());
        }
        if (reqVO.getCourseCoverUrl() != null) {
            wrapper.eq(CourseCommodityDO::getCourseCoverUrl, reqVO.getCourseCoverUrl());
        }
        if (reqVO.getCommodityName() != null) {
            wrapper.eq(CourseCommodityDO::getCommodityName, reqVO.getCommodityName());
        }
        if (reqVO.getStatus() != null) {
            wrapper.eq(CourseCommodityDO::getStatus, reqVO.getStatus());
        }
        if (reqVO.getCommodityPrice() != null) {
            wrapper.eq(CourseCommodityDO::getCommodityPrice, reqVO.getCommodityPrice());
        }
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(CourseCommodityDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        if (reqVO.getTeachingAllTime() != null) {
            wrapper.eq(CourseCommodityDO::getTeachingAllTime, reqVO.getTeachingAllTime());
        }
        if (reqVO.getTeachingTime() != null) {
            wrapper.eq(CourseCommodityDO::getTeachingTime, reqVO.getTeachingTime());
        }
        if (reqVO.getBeginTime() != null) {
            wrapper.eq(CourseCommodityDO::getBeginTime, reqVO.getBeginTime());
        }
        if (reqVO.getEndTime() != null) {
            wrapper.eq(CourseCommodityDO::getEndTime, reqVO.getEndTime());
        }
        if (reqVO.getVipFlag() != null) {
            wrapper.eq(CourseCommodityDO::getVipFlag, reqVO.getVipFlag());
        }
        if (reqVO.getApplicantNumber() != null) {
            wrapper.eq(CourseCommodityDO::getApplicantNumber, reqVO.getApplicantNumber());
        }
        if (reqVO.getCourseFlag() != null) {
            wrapper.eq(CourseCommodityDO::getCourseFlag, reqVO.getCourseFlag());
        }
        if (reqVO.getCreateTime() != null) {
            wrapper.eq(CourseCommodityDO::getCreateTime, reqVO.getCreateTime());
        }
        wrapper.orderByDesc(CourseCommodityDO::getCreateTime);
        return selectJoinList(CourseCommodityDTO.class, wrapper);
    }

}
